package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

/**
 * 一点资讯文章解析
 * Created by Administrator on 2016/7/12.
 */
@Service
public class YiDianZiXunParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.YIDIANZIXUN_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "div.content-hd h2,h2.joke-title,h2";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "div.meta>span";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "p,div.content-bd";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.content-bd img[src],div.content-bd>p>span img[src]";
    }


    @Override
    protected String authorNameSelect() {
        return "div.meta>span";
    }

    @Override
    protected void setAuthorName(Document doc, Article article) {
        Elements elements = doc.select(authorNameSelect());
        String authorName;
      if(elements.size()>2){
          authorName = doc.select("div.meta>a").first().text();
      }else{
          authorName = getAuthorName(doc);
      }

      article.setAuthorName(authorName);

    }

    @Override
    protected void setPostDate(Document doc, Article article) {
       String data = doc.select(dateSelect()).get(1).text();
       article.setPostTime(DateUtil.transfer(data));
    }

    @Override
    protected String platform() {
        return "一点资讯";
    }
}
